#include "family.h"

// 查找成员
FamilyMember *FamilyTree::findMemberByName(const std::string &name)
{
    // std::cout << "查找成员: " << name << std::endl; // 调试信息
    auto it = memberMap.find(name);
    if (it != memberMap.end())
    {
        // std::cout << "找到成员: " << it->second->name << std::endl; // 调试信息
        return it->second;
    }
    else
    {
        // std::cout << "成员未找到: " << name << std::endl; // 调试信息
        return nullptr;
    }
}
// 按名字查
//  void FamilyTree::searchByName(std::string name) {
//      // This is a simplified version and assumes a linear search
//      // In a real system, you might use a map or other data structure for faster lookup
//      if (root != nullptr && root->name == name) {
//          std::cout << "姓名: " << root->name << ", 出生日期: " << root->birthDate << ", 联系信息: " << root->contactInfo << std::endl;
//      } else {
//          std::cout << "成员未找到。" << std::endl;
//      }
//  }
bool FamilyTree::searchByBirthDateHelper(FamilyMember *node, const std::string &birthDate)
{
    if (node == nullptr)
    {
        return false;
    }
    if (node->birthDate == birthDate)
    {
        std::cout << "姓名: " << node->name << ", 出生日期: " << node->birthDate << ", 联系信息: " << node->contactInfo << ", 婚否: " << (node->isMarried ? "已婚" : "未婚") << ", 健在否: " << (node->isAlive ? "健在" : "已故") << ", 死亡日期: " << node->deathDate << std::endl;
        return true;
    }
    for (auto child : node->children)
    {
        if (searchByBirthDateHelper(child, birthDate))
        {
            return true;
        }
    }
    return false;
}

// 按出生日期查
void FamilyTree::searchByBirthDate(std::string birthDate)
{
    // bool found = false;
    // found = searchByBirthDateHelper(root, birthDate);
    if (!searchByBirthDateHelper(root, birthDate))
    {
        std::cout << "成员未找到。" << std::endl;
    }
}

// 输入两人姓名，确定其关系
void FamilyTree::determineRelationship(const std::string &name1, const std::string &name2)
{
    if (root == nullptr)
    {
        std::cout << "家谱为空。" << std::endl;
        return;
    }

    std::vector<FamilyMember *> path1, path2;
    // 使用姓名查找路径
    if (!findMemberPath(root, name1, path1) || !findMemberPath(root, name2, path2))
    {
        std::cout << "未找到其中一位成员。" << std::endl;
        return;
    }

    // 找到共同祖先
    size_t commonAncestorIndex = 0;
    while (commonAncestorIndex < path1.size() && commonAncestorIndex < path2.size() && path1[commonAncestorIndex] == path2[commonAncestorIndex])
    {
        commonAncestorIndex++;
    }

    // 确定关系
    if (commonAncestorIndex == path1.size())
    {
        std::cout << name1 << " 是 " << name2 << " 的祖先。" << std::endl;
    }
    else if (commonAncestorIndex == path2.size())
    {
        std::cout << name2 << " 是 " << name1 << " 的祖先。" << std::endl;
    }
    else
    {
        int distance1 = path1.size() - commonAncestorIndex;
        int distance2 = path2.size() - commonAncestorIndex;
        if (distance1 == 1 && distance2 == 1)
        {
            std::cout << name1 << " 和 " << name2 << " 是兄弟姐妹。" << std::endl;
        }
        else if (distance1 == 1)
        {
            std::cout << name1 << " 是 " << name2 << " 的父母。" << std::endl;
        }
        else if (distance2 == 1)
        {
            std::cout << name2 << " 是 " << name1 << " 的父母。" << std::endl;
        }
        else
        {
            std::cout << name1 << " 和 " << name2 << " 是远亲。" << std::endl;
        }
    }
}